<script lang="jsx" setup>
import { ref } from 'vue';
import Form from '@/components/common/form.vue';
import { lotNoSearch, moldrecord } from '@/service/api/modules/workwear/mold-management';

const moldNoList = ref([]);
function isMOldNoList() {
  moldrecord().then(({ error, data }) => {
    if (!error) {
      console.log(data);
      // moldNoList.value = data.list;
      moldNoList.value = data.list.map(item => {
        return {
          value: item.moldNo,
          label: item.moldNo
        };
      });
    }
  });
}
isMOldNoList();

const formList = [
  {
    type: 'tabs',
    name: '基本项目',
    span: 24
  },
  {
    type: 'searchInput',
    key: 'lotNo',
    label: 'LOT号',
    placeholder: '请输入LOT号',

    span: 12,
    rule: {
      required: true,
      message: '请输入模具型号',
      trigger: 'blur'
    },
    handleSearch() {
      lotNoSearch(model.value.lotNo).then(({ data, error }) => {
        if (!error) {
          // moldNoList.value = data.map(item => {
          //   return {
          //     value: item.moldNo,
          //     label: item.moldNo
          //   };
          // });
          model.value.punchNum = data.setQty;
        }
      });
    }
  },
  {
    type: 'select',
    key: 'moldNo',
    label: '模具型号',
    placeholder: '请选择模具型号',
    search: true,
    span: 12,
    rule: {
      required: true,
      message: '请选择模具类型',
      trigger: 'blur'
    },
    selectOptions: moldNoList
  },

  {
    type: 'input',
    key: 'punchNum',
    label: '使用次数',
    placeholder: '请输入使用次数',
    span: 12
  },

  {
    span: 12,
    type: 'datePicker',
    key: 'dateTime',
    label: '日期',
    rule: {
      required: true,
      message: '请选择日期',
      trigger: 'change'
    },
    tansfromDisabled(operateType) {
      return operateType === 'look';
    }
  },
  {
    type: 'select',
    key: 'moldState',

    label: '是否研磨',
    span: 12,
    selectOptions: [
      {
        value: '0',
        label: '否'
      },
      {
        value: '1',
        label: '是'
      }
    ]
  },

  {
    type: 'input',
    key: 'grindNum',
    label: '研磨次数',
    placeholder: '请输入研磨次数',
    span: 12
  },

  {
    type: 'input',
    key: 'operator',
    label: '作业员',
    rule: {
      required: true,
      message: '请输入作业员',
      trigger: 'blur'
    },
    placeholder: '请输入作业员',
    span: 12
  },

  {
    type: 'textarea',
    key: 'remark',
    label: '备注',
    placeholder: '请输入备注',
    span: 12,
    textareaRows: 1
  }
];
const model = ref(getModel());
function getModel() {
  const obj = {};
  formList.forEach(item => {
    if (item.key && item.span) {
      obj[item.key] = null;
    }
  });
  return obj;
}
const formRef = ref(null);

function validateForm() {
  return formRef.value.validateForm();
}

defineExpose({
  model: model.value,
  validateForm
});
</script>

<template>
  <div class="event-form">
    <Form ref="formRef" v-model:model="model" :form-list="formList"></Form>
  </div>
</template>

<style lang="scss" scoped></style>
